package com.example.springboot_blog.config;


import com.example.springboot_blog.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.sql.DataSource;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Qualifier("dataSource")
    @Autowired
    private DataSource dataSource;
    @Autowired
    UserServiceImpl userService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        auth.userDetailsService(userService).passwordEncoder(encoder);


//        String userSql = ("select username,password,active from user where username=?");
//        String authSql = ("select r.role,u.username from role r,user u,user_role ur where r.id=ur.role_id and u.id=ur.user_id and u.username=?");
//        auth.jdbcAuthentication().passwordEncoder(encoder)
//                .dataSource(dataSource)
//                .usersByUsernameQuery(userSql)
//                .authoritiesByUsernameQuery(authSql);



//        // 1、使用内存用户信息，作为测试使用
        auth.inMemoryAuthentication().passwordEncoder(encoder)
                .withUser("dwy123").password(encoder.encode("123456")).roles("vip")
                .and()
                .withUser("username").password(encoder.encode("123456")).roles("common")
                .and()
                .withUser("user1").password(encoder.encode("123456")).roles("common");
        ;

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                //具体规则放前面
                .antMatchers("/back/**").hasRole("vip")             /*.authenticated()  .hasRole("vip")  .permitAll()*/
                //注意放行静态资源和前台页面路径
                .antMatchers("/assets/**", "/css/**", "/", "/article/**").permitAll()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated();

        http.formLogin()
                .loginPage("/login").permitAll()
                .usernameParameter("username")
                .passwordParameter("password")
                .defaultSuccessUrl("/")
                .failureUrl("/login?error");

        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/");

        http.rememberMe()
                .rememberMeParameter("rememberme")
                .tokenValiditySeconds(200);
    }
}
